package com.hisense.ovcloud.warehouse.tool;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BeanListConverter {
    public static <T> List<T> convertToBeans(ResultSet rs, Class<T> beanClass) throws SQLException, InstantiationException, IllegalAccessException {
        List<T> beans = new ArrayList<>();
        while (rs.next()) {
            T bean = beanClass.newInstance();
            for (java.lang.reflect.Field field : beanClass.getDeclaredFields()) {
                field.setAccessible(true);
                String fieldName = field.getName();
                Object fieldValue = rs.getObject(fieldName);
                if(null != fieldValue){
                    field.set(bean, fieldValue);
                }
            }
            beans.add(bean);
        }
        return beans;
    }
}
